/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.PayApplyPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  PayApplyPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class PayApplySqlProvider {
	public static final String TABLE_NAME = "pay_apply";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param payApply
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findPayApplys(final PayApplyPo payApply) {
		return new CustomerSQL() {{
			SELECT("amount,amount_actual,create_time,creator_id,cur_name,cus_id,desciption,id,order_code,pay_method_id,pay_method_name,pay_method_icon,receive_time,request_time,status,update_time,updater_id,pay_method_icon ");
			FROM(TABLE_NAME);
			
			if (null != payApply.getAmount()) {
				WHERE("amount=#{amount}");
			}
			if (null != payApply.getAmountActual()) {
				WHERE("amount_actual=#{amountActual}");
			}
			if (null != payApply.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(payApply.getCurName())) {
				WHERE("cur_name=#{curName}");
			}
			if (StringUtils.isNotBlank(payApply.getCusId())) {
				WHERE("cus_id=#{cusId}");
			}
			if (StringUtils.isNotBlank(payApply.getDesciption())) {
				WHERE("desciption=#{desciption}");
			}
			if (StringUtils.isNotBlank(payApply.getOrderCode())) {
				WHERE("order_code=#{orderCode}");
			}
			if (null != payApply.getPayMethodId()) {
				WHERE("pay_method_id=#{payMethodId}");
			}
			if (null != payApply.getReceiveTime()) {
				WHERE("receive_time=#{receiveTime}");
			}
			if (null != payApply.getRequestTime()) {
				WHERE("request_time=#{requestTime}");
			}
			if (null != payApply.getStatus()) {
				WHERE("status=#{status}");
			}
			if (null != payApply.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if(StringUtils.isNotBlank(payApply.getSortField())) {
				ORDER_BY(payApply.getSortField() + (StringUtils.isNotBlank(payApply.getSortMethod()) ? "desc" : payApply.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != payApply.getStart() && null != payApply.getSize()) {
				LIMIT(payApply.getStart(), payApply.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param payApply
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findPayApplyCount(final PayApplyPo payApply) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (null != payApply.getAmount()) {
				WHERE("amount=#{amount}");
			}
			if (null != payApply.getAmountActual()) {
				WHERE("amount_actual=#{amountActual}");
			}
			if (null != payApply.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(payApply.getCurName())) {
				WHERE("cur_name=#{curName}");
			}
			if (StringUtils.isNotBlank(payApply.getCusId())) {
				WHERE("cus_id=#{cusId}");
			}
			if (StringUtils.isNotBlank(payApply.getDesciption())) {
				WHERE("desciption=#{desciption}");
			}
			if (StringUtils.isNotBlank(payApply.getOrderCode())) {
				WHERE("order_code=#{orderCode}");
			}
			if (null != payApply.getPayMethodId()) {
				WHERE("pay_method_id=#{payMethodId}");
			}
			if (StringUtils.isNotBlank(payApply.getPayMethodName())) {
				WHERE("pay_method_name=#{payMethodName}");
			}
			if (null != payApply.getReceiveTime()) {
				WHERE("receive_time=#{receiveTime}");
			}
			if (null != payApply.getRequestTime()) {
				WHERE("request_time=#{requestTime}");
			}
			if (null != payApply.getStatus()) {
				WHERE("status=#{status}");
			}
			if (null != payApply.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findPayApplyById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param payApply
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addPayApply(final PayApplyPo payApply) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (null != payApply.getAmount()) {
				VALUES("amount","#{amount}");
			}
			if (null != payApply.getAmountActual()) {
				VALUES("amount_actual","#{amountActual}");
			}
			if (null != payApply.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(payApply.getCurName())) {
				VALUES("cur_name","#{curName}");
			}
			if (StringUtils.isNotBlank(payApply.getCusId())) {
				VALUES("cus_id","#{cusId}");
			}
			if (StringUtils.isNotBlank(payApply.getDesciption())) {
				VALUES("desciption","#{desciption}");
			}
			if (StringUtils.isNotBlank(payApply.getOrderCode())) {
				VALUES("order_code","#{orderCode}");
			}
			if (null != payApply.getPayMethodId()) {
				VALUES("pay_method_id","#{payMethodId}");
			}
			if (StringUtils.isNotBlank(payApply.getPayMethodName())) {
				VALUES("pay_method_name","#{payMethodName}");
			}
			if (null != payApply.getRequestTime()) {
				VALUES("request_time","#{requestTime}");
			}
			if (null != payApply.getStatus()) {
				VALUES("status","#{status}");
			}
			if (null != payApply.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
			if (StringUtils.isNotBlank(payApply.getPayMethodIcon())) {
				VALUES("pay_method_icon","#{payMethodIcon}");
			}
			VALUES("receive_time","now()");
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param payApply
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updatePayApply(final PayApplyPo payApply) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (null != payApply.getAmount()) {
				SET("amount=#{amount}");
			}
			if (null != payApply.getAmountActual()) {
				SET("amount_actual=#{amountActual}");
			}
			if (null != payApply.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(payApply.getCurName())) {
				SET("cur_name=#{curName}");
			}
			if (StringUtils.isNotBlank(payApply.getCusId())) {
				SET("cus_id=#{cusId}");
			}
			if (StringUtils.isNotBlank(payApply.getDesciption())) {
				SET("desciption=#{desciption}");
			}
			if (StringUtils.isNotBlank(payApply.getOrderCode())) {
				SET("order_code=#{orderCode}");
			}
			if (null != payApply.getPayMethodId()) {
				SET("pay_method_id=#{payMethodId}");
			}
			if (StringUtils.isNotBlank(payApply.getPayMethodName())) {
				SET("pay_method_name=#{payMethodName}");
			}
			if (null != payApply.getReceiveTime()) {
				SET("receive_time=#{receiveTime}");
			}
			if (null != payApply.getRequestTime()) {
				SET("request_time=#{requestTime}");
			}
			if (null != payApply.getStatus()) {
				SET("status=#{status}");
			}
			if (null != payApply.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			if (StringUtils.isNotBlank(payApply.getPayMethodIcon())) {
				SET("pay_method_icon=#{payMethodIcon}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delPayApply(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
